<?php

namespace common\models;

use common\helpers\StringUtil;
use Yii;
use yii\behaviors\AttributeBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "log".
 *
 * @property integer $id
 * @property string $action
 * @property string $old_content
 * @property string $new_content
 * @property string $memo
 * @property string $all_content
 * @property string $created_by
 * @property string $created_at
 * @property string $updated_by
 * @property string $updated_at
 */
class Log extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'log';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['action'], 'required'],
            [['created_at', 'updated_at'], 'safe'],
            [['action'], 'string', 'max' => 255],
            [['old_content', 'new_content', 'memo'], 'string', 'max' => 2048],
            [['all_content'], 'string', 'max' => 1024],
            [['created_by', 'updated_by'], 'string', 'max' => 45],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'action' => Yii::t('app', 'Action'),
            'old_content' => Yii::t('app', 'Old Content'),
            'new_content' => Yii::t('app', 'New Content'),
            'memo' => Yii::t('app', 'Memo'),
            'all_content' => Yii::t('app', 'All Content'),
            'created_by' => Yii::t('app', 'Created By'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_by' => Yii::t('app', 'Updated By'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }

    public function behaviors()
    {
        return [
            [
                'class' => AttributeBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => 'all_content',
                    ActiveRecord::EVENT_BEFORE_UPDATE => 'all_content',
                ],
                'value' => function ($event) {
                    $all = sprintf('action:[%s], old_content:[%s], new_content:[%s], memo[%s]', $this->action, $this->old_content, $this->new_content, $this->memo);
                    $all = StringUtil::getShortString($all, 1024);
                    return $all;
                },
            ],
        ];
    }
}
